﻿#Include ..\baseHandle.ahk

#Include ..\..\_lib\Unicodes.ahk
#Include ..\..\_lib\JSON.ahk
#Include ..\..\_lib\Path.ahk
#Include ..\..\_lib\RunCMD.ahk

class Trans_bd extends BaseHandle {

  static Handle(parsed) {
    if (o := DoRequest(data := parsed.target)).Get('errno') != 0
      return this.Fail('failure on' data)
    return this.Succ(!o.Get('data').Length ? 'no result' : ToString(o.Get('data')[1]['v']))
    DoRequest(body) {
      _p := Path.join('./cfg/translate/_.py')
      loop 5 {
        if JSON.Parse(StrReplace(res := RunCMD(Format('py {} {}', _p, body)), "'", '"'), true).Get('errno') = 0
          break
        Sleep 300
      }
      return JSON.parse(StrReplace(DecodeUniCodeString(res), "'", '"'), true)
    }
  }

  static Echo() => '
  (
    trans_bd 百度翻译单词
    trans_bd <target>
  )'

}
